<%@ page language="java" import="com.newcrm.config.Const,com.newcrm.util.Util,com.newcrm.bean.*,java.util.List,java.util.Map,java.util.Date" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
  User user = (User) session.getAttribute("user");
  Map<Integer, CompanyInfo> companys = (Map<Integer, CompanyInfo>) application.getAttribute(Const.APPLICATION_COMPANYS);
  CompanyInfo companyInfo = companys.get(Integer.valueOf(user.companyId));
  List<Group> allGroups = companyInfo.getAllGroups();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>创建新员工</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta name="renderer" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge">
  <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/qikebao.ico" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/all.css?ver=<%=Const.VERSION%>">
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/datepicker.css" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/bootstrap-datetimepicker.min.css" />
  <link rel="stylesheet" href="<%=request.getContextPath()%>/css/magnific-popup.css">
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-1.11.1.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery-ui.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.bootstrap-growl.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/moment.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap-datepicker.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/bootstrap-datetimepicker.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/daterangepicker.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.magnific-popup.min.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/jquery.cookie.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/popbox.js"></script>
  <script type="text/javascript" src="<%=request.getContextPath()%>/script/common.js?ver=<%=Const.VERSION%>"></script>
  <script>
    $(function() {
      $('.datepickerinput').datepicker({todayHighlight:true,language:'zh-CN'});
      var isExistName = false;
      $("#identity_group").val('<%=request.getAttribute("groupId")%>');
      <%if (user.isAdmin) { %>
      if ($("#identity_group").val() == "0") {
        $("#isGroupAdminSpan").hide();
      } else {
        $("#isGroupAdminSpan").show();
      }
      <%}%>

      $("#identity_userId").blur(function(){
        var params = {};
        params.userId = $("#identity_userId").val();
        if (params.userId == "") {
          return;
        }
        isExistName = false;
        $.ajax({
          url: '<%=request.getContextPath()%>/users/checkUserId?random=' + Math.random(),
          type: 'GET',
          data:params,
          dataType: 'json',
          timeout: 60000,
          error: function(){
          },
          success: function(json){
            if (json.result == "1") {
              $("#identity_userId").parent().parent().removeClass("invalid").addClass("valid");
              isExistName = false;
            } else {
              $("#identity_userId").parent().parent().addClass("invalid");
              $("#identity_userId").parent().next().text("该登录ID已经存在，请选择其他的ID");
              isExistName = true;
            }
          }
        });
      });
      <%if (user.isAdmin) { %>
      $('#identity_group').change(function() {
        if ($(this).val() == "0") {
          $("#isGroupAdminSpan").hide();
        } else {
          $("#isGroupAdminSpan").show();
        }
      });
      <%}%>
      $("#createuser-link").click(function(e) {
        e.preventDefault();
        $('#identity_form').submit();
      });

      $("#identity_form").submit(function(event){
        if (isExistName) {
          return false;
        }
        if ($("#identity_userId").val() == "") {
          $("#identity_userId").focus();
          $("#identity_userId").parent().parent().addClass("invalid");
          $("#identity_userId").parent().next().text("请输入登录ID");
          return false;
        }
        // 只能是英文和数字，并且在 20 个字符以内
        if (!checkDigital($("#identity_userId").val())) {
          $("#identity_userId").focus();
          $("#identity_userId").parent().parent().addClass("invalid");
          $("#identity_userId").parent().next().text("登录ID必须为英文或者数字的组合，且第一个为字母，最大长度20个字符");
          return false;
        }
        $("#identity_userId").parent().parent().removeClass("invalid");
        $("#identity_userId").parent().next().text("");

        if (!checkEmail($("#identity_email").val())) {
          $("#identity_email").focus();
          $("#identity_email").parent().parent().addClass("invalid");
          $("#identity_email").parent().next().text("输入的邮箱格式不正确");
          return false;
        }
        $("#identity_email").parent().parent().removeClass("invalid");
        $("#identity_email").parent().next().text("");

        if ($("#identity_name").val() == "") {
          $("#identity_name").focus();
          $("#identity_name").parent().parent().addClass("invalid");
          $("#identity_name").parent().next().text("请输入员工姓名");
          return false;
        }
        $("#identity_name").parent().parent().removeClass("invalid");
        $("#identity_name").parent().next().text("");

        if (isNaN($("#maxcompany_num").val())) {
          $("#maxcompany_num").focus();
          $("#maxcompany_num").parent().parent().addClass("invalid");
          $("#maxcompany_num").parent().next().text("请输入数字");
          return false;
        }
        $("#maxcompany_num").parent().parent().removeClass("invalid");
        $("#maxcompany_num").parent().next().text("");

        var identity_password = $("#identity_password").val();
        var identity_password_confirmation = $("#identity_password_confirmation").val();

        if (identity_password == "") {
          $("#identity_password").focus();
          $("#identity_password").parent().parent().addClass("invalid");
          $("#identity_password").parent().next().text("请输入员工登录密码");
          return false;
        }

        if (identity_password != identity_password_confirmation) {
          $("#identity_password_confirmation").focus();
          $("#identity_password_confirmation").parent().parent().addClass("invalid");
          $("#identity_password_confirmation").parent().next().text("密码和确认密码不一致");
          return false;
        }

        $("#identity_password_confirmation").parent().parent().removeClass("invalid");
        $("#identity_password_confirmation").parent().next().text("");

        $("#createuser-link").parent().addClass("busy");
        return true;
      });
    });
  </script>
</head>
<body class="parties edit_identity">
  <jsp:include page="topNavi.jsp" flush="true"/>
  <div class="main-page">
    <table class="setting">
    <tr>
      <td class="main-body">
        <div class="topnavi">
          <div class='leftMenu'><span>创建新员工</span></div>
        </div>
        <div class="body">
        <form action="<%=request.getContextPath()%>/users/add" id="identity_form" class="identity_form" enctype="multipart/form-data" method="post">
          <br/>
          <table class="setting">
            <tr class="avatar">
              <th><img alt="Avatar" src="<%=request.getContextPath()%>/images/avatar_person.png" /></th>
              <td>
                <div class="change_avatar">
                  <p><label>更改头像</label></p>
                  <p><input id="identity_avatar" name="identity_avatar" size="30" type="file" /></p>
                </div>
              </td>
            </tr>
            <tr>
              <th><label for="identity_userId">登录ID</label></th>
              <td>
                <div class="validated_field">
                  <p class="field"><input class="normal" id="identity_userId" name="identity_userId" size="30" tabindex="1" type="text" value="" /></p>
                  <p class="error"></p>
                </div>
              </td>
            </tr>
            <tr>
              <th><label for="identity_name">姓名</label></th>
              <td>
                <div class="validated_field">
                  <p class="field"><input class="normal" id="identity_name" name="identity_name" size="30" tabindex="2" type="text" value="" /></p>
                  <p class="error"></p>
                </div>
              </td>
            </tr>
            <tr>
              <th><label for="identity_sex">性别</label></th>
              <td>
                <div class="validated_field">
                  <p class="field">
                    <select id="identity_sex" name="identity_sex" tabindex="3">
                    <option value="1">男</option>
                    <option value="0">女</option>
                    </select>
                  <p class="error"></p>
                </div>
              </td>
            </tr>
            <tr>
              <th><label for="identity_hireDate">入职时间</label></th>
              <td>
                <div class="validated_field">
                  <input type="text" value="<%=Util.date2Str(new Date())%>" id="hire_date" tabindex="4" class="normal datepickerinput">
                </div>
              </td>
            </tr>
            <tr>
              <th><label for="identity_id">证件号</label></th>
              <td>
                <div class="validated_field">
                  <p class="field"><input class="normal" id="identity_id" name="identity_id" size="30" tabindex="5" type="text" value="" /></p>
                  <p class="error"></p>
                </div>
              </td>
            </tr>
            <tr>
              <th><label for="identity_mobile">手机</label></th>
              <td>
                <div class="validated_field">
                  <p class="field"><input class="normal" id="identity_mobile" name="identity_mobile" size="30" tabindex="6" type="text" value="" /></p>
                  <p class="error"></p>
                </div>
              </td>
            </tr>
            <tr>
              <th><label for="identity_qq">QQ号</label></th>
              <td>
                <div class="validated_field">
                  <p class="field"><input class="normal" id="identity_qq" name="identity_qq" size="30" tabindex="7" type="text" value="" /></p>
                  <p class="error"></p>
                </div>
              </td>
            </tr>
            <tr>
              <th><label for="identity_email">邮箱</label></th>
              <td>
                <div class="validated_field">
                  <p class="field"><input class="normal" id="identity_email" name="identity_email" size="30" tabindex="8" type="text" value="" /></p>
                  <p class="error"></p>
                </div>
              </td>
            </tr>
            <tr>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <th><label for="identity_name">部门</label></th>
              <td>
                <div class="validated_field">
                  <p class="field"><select id="identity_group" name="identity_group" tabindex="9">
                    <%if (user.isAdmin) { %>
                    <option value="0">无</option>
                    <%  if (allGroups != null && allGroups.size() > 0) {
                          for (Group group:allGroups) {%>
                    <option value="<%=group.getId()%>"><%=Util.HTMLEncode(group.getName())%></option>
                    <%    }
                        }
                      } else {
                        Group group = companyInfo.getGroupById(user.groupId); %>
                        <option value="<%=group.getId()%>"><%=Util.HTMLEncode(group.getName())%></option>
                    <%
                        List<Group> childrenGroups = group.getAllChildren();
                        if (childrenGroups != null) {
                          for (Group childGroup:childrenGroups) {%>
                    <option value="<%=childGroup.getId()%>"><%=Util.HTMLEncode(childGroup.getName())%></option>
                    <%}}}%>
                    </select>
                    <%if (user.isAdmin) { %><span id="isGroupAdminSpan" style="display:none;"><input type="checkbox" name="identity_isGroupAdmin" id="identity_isGroupAdmin" value="1"><label for="identity_isGroupAdmin">部门主管</label></span><%}%>
                    </p>
                  <p class="error"></p>
                </div>
              </td>
            </tr>
            <tr>
              <th><label for="maxcompany_num">客户上限</label></th>
              <td>
                <div class="validated_field">
                  <p class="field"><input id="maxcompany_num" name="maxcompany_num" size="20" class="normal" tabindex="10" type="text" value="0" />（0或者不设置表示无限制）</p>
                  <p class="error"></p>
                </div>
              </td>
            </tr>
            <tr>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tbody class="signal_id_credentials">
              <tr class="username_password">
                <th><label for="identity_password">密码</label></th>
                <td>
                  <div class="validated_field">
                    <p class="field"><input class="dummy" id="identity_password" name="identity_password" size="30" tabindex="11" type="password" /></p>
                    <p class="error"></p>
                  </div>
                </td>
              </tr>
              <tr class="username_password confirm_password">
                <th><label for="identity_password_confirmation">确认密码</label></th>
                <td>
                  <div class="validated_field">
                    <p class="field"><input class="dummy" id="identity_password_confirmation" name="identity_password_confirmation" size="30" tabindex="12" type="password" /></p>
                    <p class="error"></p>
                  </div>
                </td>
              </tr>
            </tbody>
            <tr class="submit">
              <th></th>
              <td>
                <a id="createuser-link" class="button blue" href="#"><i class="fa fa-plus"></i> 创建新员工</a>
                &nbsp;&nbsp; <a href="<%=request.getContextPath()%>/users?groupId=<%=request.getAttribute("groupId")%>" class="admin">取 消</a>
              </td>
            </tr>
          </table>
        </form>
        </div>
      </td>
      <td class="main-sidebar">
        <div class="content">
          <div class="liner">
            <div class="sidebox closedbox">
              <h5>给员工创建新的账号</h5>
              <p>可以给每个员工创建一个新的账号，其中登录ID，可以是员工的员工编号，姓名的拼音，或者英文名字，方便记忆和区分。</p>
            </div>
          </div>
        </div>
      </td>
    </tr>
    </table>
  </div>
</body>
</html>